class DictParamChecker:
    '''用于接口的参数检查，可用于URL、单层字典参数
    检查参数必选、可选、默认值、类型
    所有传入参数都是字符串类型，适用于检查http中的参数'''
    def __init__(self):
        self.paramInfo = {}

    def addParam(self, name, valueType, required, defaultValue=None, **kwargs):
        '''name: 参数名
        valueType: 参数值字符串对应的类型，支持：'str', 'int', 'float', '01'
        required: 是否必选
        defaultValue: 默认值，对于必选参数无效，如果设置为'__NOT_SET__'则表示不填入默认值'''
        if type(name) != str:
            raise ValueError('name is not string!')
        if type(valueType) != str:
            raise ValueError('valueType is not type!')
        if type(required) != bool:
            raise ValueError('required is not bool!')
        self.paramInfo[name] = {'type':valueType, 'required':required, 'default':defaultValue, 'max_len':None, 'min_len':None}
        if 'max_len' in kwargs:
            self.paramInfo[name]['max_len'] = kwargs['max_len']
        if 'min_len' in kwargs:
            self.paramInfo[name]['min_len'] = kwargs['min_len']

    def check(self, params, transType=False):
        ''' 根据设定的参数，检查传入params
        成功时返回处理后的params，否則返回錯誤信息字符串
        transType: 是否需要把字符串类型转换成对应的类型'''
        if type(params) != dict:
            return 'type of params is not dict'
        for name in self.paramInfo:
            # 檢查參數是否是必填參數
            if self.paramInfo[name]['required'] == True:
                if name not in params:
                    return 'param %s is missing'%(name)
            else:
                if name not in params:
                    if self.paramInfo[name]['default'] == '__NOT_SET__':
                        continue
                    else:
                        params[name] = self.paramInfo[name]['default']
            new_value = params[name]
            # 檢查參數類型
            valueType = self.paramInfo[name]['type']
            try:
                if valueType == 'str':
                    if type(params[name]) != str:
                        raise TypeError()
                elif valueType == 'int':
                    new_value = int(params[name])
                elif valueType == 'float':
                    new_value = float(params[name])
                elif valueType == '01':
                    new_value = int(params[name])
                    if new_value != 0 and new_value != 1:
                        raise TypeError()
                else:
                    raise ValueError('unsupport type to check: ' + valueType)
            except:
                return 'type of %s is not correct'%(name)
            if transType == True:
                params[name] = new_value
        return params

